package com.mlethe.library.multi.api;

import com.alipay.share.sdk.openapi.BaseReq;
import com.alipay.share.sdk.openapi.BaseResp;
import com.alipay.share.sdk.openapi.IAPAPIEventHandler;
import com.mlethe.library.multi.api.listener.OnActionCallback;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝回调处理类
 *
 * @Author Mlethe
 * @Date 2025/3/28
 */
final class AlipayEventHandler implements IAPAPIEventHandler {

    private OnActionCallback mOnActionCallback;

    public AlipayEventHandler(OnActionCallback callback) {
        this.mOnActionCallback = callback;
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        MultiMedia media = MultiMedia.ALIPAY;
        OnActionCallback callback = mOnActionCallback;
        mOnActionCallback = null;
        int errCode = baseResp.errCode;
        switch (errCode) {
            case BaseResp.ErrCode.ERR_OK:
                // 分享成功
                Map<String, String> map = new HashMap<>();
                if (callback != null) {
                    callback.onComplete(media, map);
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                // 分享取消
                if (callback != null) {
                    callback.onCancel(media);
                }
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                // 权限验证失败
                if (callback != null) {
                    callback.onFailure(media, errCode, baseResp.errStr);
                }
                break;
            case BaseResp.ErrCode.ERR_SENT_FAILED:
                // 分享失败
                if (callback != null) {
                    callback.onFailure(media, errCode, baseResp.errStr);
                }
                break;
            default:
                // 未知错误
                if (callback != null) {
                    callback.onFailure(media, errCode, baseResp.errStr);
                }
                break;
        }
    }
}
